﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Xml . Linq;

namespace WenceyWang . RichMan4L . Maps
{
	public class ReturnRoad : Road
	{
		long exitID;

		Road exit;

		public Road Exit { get { return exit ?? ( exit = Map . Currnet . GetRoad ( exitID ) ); } private set { exit = value; } }

		public override Path Route ( Road previous , int moveCount , Path result = null )
		{
			if ( previous == null )
			{
				throw new ArgumentNullException ( nameof ( previous ) );
			}
			if ( !CanEnter ( previous ) )
			{
				throw new ArgumentException ( "无法通过previous进入此道路" , nameof ( previous ) );
			}
			if ( moveCount < 0 )
			{
				throw new ArgumentOutOfRangeException ( nameof ( moveCount ) );
			}
			Path current = result ?? new Path ( );
			current . Route . Add ( this );
			return Exit . Route ( this , moveCount - 1 , current );
		}

		protected override void Dispose ( bool disposing )
		{
			Exit = null;
			base . Dispose ( disposing );
		}

		public override bool CanEnter ( Road road ) { return road == Exit; }

		public ReturnRoad ( XElement resource ) : base ( resource )
		{
			exitID = Convert . ToInt64 ( resource . Attribute ( nameof ( Exit ) ) . Value );
		}
	}
}
